ios中模拟手势单击或是双击 |
您所在的位置:网站首页 › ios 模拟手势 › ios中模拟手势单击或是双击 |
1.拓展UITouch类和UIEvent类(通过category),具体见附件TouchSynthesis.h和TouchSynthesis.m 2.在需要的类中,定义UITouch和UIEvent类的对象。 UITouch *touch = [[UITouchalloc] initWithPoint:point andView:mapview]; UIEvent *eventDown = [[UIEventalloc] initWithTouch:touch]; [touch.view touchesBegan:[eventDownallTouches] withEvent:eventDown];
[touch setPhase:UITouchPhaseEnded]; UIEvent *eventUp = [[UIEventalloc] initWithTouch:touch]; [touch.view touchesEnded:[eventUp allTouches] withEvent:eventUp]; [eventDown release]; [eventUp release]; [touch release];
TouchSynthesis.h文件 #import @interface UITouch (Synthesize) - (id)initInView:(UIView *)view; -(id) initdoubletap:(UIView *)view; - (id)initWithPoint:(CGPoint)point andView:(UIView*)view; - (void)setPhase:(UITouchPhase)phase; - (void)setLocationInWindow:(CGPoint)location; @end @interface UIEvent (Synthesize) - (id)initWithTouch:(UITouch *)touch; @end TouchSynthesis.m文件 #import "TouchSynthesis.h" #import "MyUITouch.h" @implementation UITouch (Synthesize) // // initInView:phase: // // Creats a UITouch, centered on the specified view, in the view's window. // Sets the phase as specified. // - (id)initInView:(UIView *)view { NSLog(@"init in view"); self = [super init]; if (self != nil) { CGRect frameInWindow; if ([view isKindOfClass:[UIWindow class]]) { frameInWindow = view.frame; } else { frameInWindow = [view.window convertRect:view.frame fromView:view.superview]; } _tapCount = 1; _locationInWindow = CGPointMake( frameInWindow.origin.x + 0.5 * frameInWindow.size.width, frameInWindow.origin.y + 0.5 * frameInWindow.size.height); _previousLocationInWindow = _locationInWindow; UIView *target = [view.window hitTest:_locationInWindow withEvent:nil]; _window = [view.window retain]; _view = [target retain]; _phase = UITouchPhaseBegan; _touchFlags._firstTouchForView = 1; _touchFlags._isTap = 1; _timestamp = [NSDate timeIntervalSinceReferenceDate]; } return self; } - (id)initdoubletap:(UIView *)view { NSLog(@"init in view"); self = [super init]; if (self != nil) { CGRect frameInWindow; if ([view isKindOfClass:[UIWindow class]]) { frameInWindow = view.frame; } else { frameInWindow = [view.window convertRect:view.frame fromView:view.superview]; } _tapCount = 2; _locationInWindow = CGPointMake( frameInWindow.origin.x + 0.5 * frameInWindow.size.width, frameInWindow.origin.y + 0.5 * frameInWindow.size.height); _previousLocationInWindow = _locationInWindow; UIView *target = [view.window hitTest:_locationInWindow withEvent:nil]; _window = [view.window retain]; _view = [target retain]; _phase = UITouchPhaseBegan; _touchFlags._firstTouchForView = 1; _touchFlags._isTap = 1; _timestamp = [NSDate timeIntervalSinceReferenceDate]; } return self; } - (id)initWithPoint:(CGPoint)point andView:(UIView*)view { self = [super init]; if (self != nil) { CGRect frameInWindow; if ([view isKindOfClass:[UIWindow class]]) { frameInWindow = view.frame; } else { frameInWindow = [view.window convertRect:view.frame fromView:view.superview]; } _tapCount = 1; _locationInWindow = point; _previousLocationInWindow = _locationInWindow; UIView *target = [view.window hitTest:_locationInWindow withEvent:nil]; _view = [target retain]; _window = [view.window retain]; _phase = UITouchPhaseBegan; _touchFlags._firstTouchForView = 1; _touchFlags._isTap = 1; _timestamp = [NSDate timeIntervalSinceReferenceDate]; } return self; } // // setPhase: // // Setter to allow access to the _phase member. // - (void)setPhase:(UITouchPhase)phase { _phase = phase; _timestamp = [NSDate timeIntervalSinceReferenceDate]; } // // setPhase: // // Setter to allow access to the _locationInWindow member. // - (void)setLocationInWindow:(CGPoint)location { _previousLocationInWindow = _locationInWindow; _locationInWindow = location; _timestamp = [NSDate timeIntervalSinceReferenceDate]; } @end // // GSEvent is an undeclared object. We don't need to use it ourselves but some // Apple APIs (UIScrollView in particular) require the x and y fields to be present. // @interface GSEventProxy : NSObject { @public unsigned int flags; unsigned int type; unsigned int ignored1; float x1; float y1; float x2; float y2; unsigned int ignored2[10]; unsigned int ignored3[7]; float sizeX; float sizeY; float x3; float y3; unsigned int ignored4[3]; } @end @implementation GSEventProxy @end // // PublicEvent // // A dummy class used to gain access to UIEvent's private member variables. // If UIEvent changes at all, this will break. // @interface PublicEvent : NSObject { @public GSEventProxy *_event; NSTimeInterval _timestamp; NSMutableSet *_touches; CFMutableDictionaryRef _keyedTouches; } @end @implementation PublicEvent @end @interface UIEvent (Creation) - (id)_initWithEvent:(GSEventProxy *)fp8 touches:(id)fp12; @end // // UIEvent (Synthesize) // // A category to allow creation of a touch event. // @implementation UIEvent (Synthesize) - (id)initWithTouch:(UITouch *)touch { CGPoint location = [touch locationInView:touch.window]; GSEventProxy *gsEventProxy = [[GSEventProxy alloc] init]; gsEventProxy->x1 = location.x; gsEventProxy->y1 = location.y; gsEventProxy->x2 = location.x; gsEventProxy->y2 = location.y; gsEventProxy->x3 = location.x; gsEventProxy->y3 = location.y; gsEventProxy->sizeX = 1.0; gsEventProxy->sizeY = 1.0; gsEventProxy->flags = ([touch phase] == UITouchPhaseEnded) ? 0x1010180 : 0x3010180; gsEventProxy->type = 3001; // // On SDK versions 3.0 and greater, we need to reallocate as a // UITouchesEvent. // Class touchesEventClass = objc_getClass("UITouchesEvent"); if (touchesEventClass && ![[self class] isEqual:touchesEventClass]) { [self release]; self = [touchesEventClass alloc]; } self = [self _initWithEvent:gsEventProxy touches:[NSSet setWithObject:touch]]; if (self != nil) { } return self; } @end MyUITouch.h文件 #import #ifdef __IPHONE_6_0 @interface UITouch () { NSTimeInterval _timestamp; UITouchPhase _phase; UITouchPhase _savedPhase; NSUInteger _tapCount; UIWindow *_window; UIView *_view; UIView *_gestureView; UIView *_warpedIntoView; NSMutableArray *_gestureRecognizers; NSMutableArray *_forwardingRecord; CGPoint _locationInWindow; CGPoint _previousLocationInWindow; UInt8 _pathIndex; UInt8 _pathIdentity; float _pathMajorRadius; struct { unsigned int _firstTouchForView:1; unsigned int _isTap:1; unsigned int _isDelayed:1; unsigned int _sentTouchesEnded:1; unsigned int _abandonForwardingRecord:1; } _touchFlags; } @end #endif
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |